#include "bsp_main.h"   
#include "string.h"
#include "esp_log.h"



void bsp_main_init()
{
    vTaskDelay(200 / portTICK_PERIOD_MS);
    my_uart0_init();
    local_nvs_init();
   

    TM1650_Init();
    temp_adc_init();
    beep_init();
    mainpwm_init();

    
    blufi_init();
    mqtt_init();
    vTaskDelay(2000 / portTICK_PERIOD_MS);
    beep_output(BEEP_START);
    
}


extern QueueHandle_t tm1650_led_queue;
void task_sysmain(void* arg)
{
    sys_main_param_t sys_main_param_old = {
        .sys_mode = MODE_MAX,
        .sys_runtime = 30*60,
        .sys_status = SYS_STOP,
        .sys_strength = STRENGTH_MAX,
        .sys_countdown = 30*60,
        .systemp = {60,70,80},
        .sys_error_type = ERROR_TYPE_NO_ERROR,
        .ifappmqttconnect = MQTT_APP_DISCONNECT,
        .ifblufibleconnect = BLUFI_BLE_DISCONNECT,
    };
    uint32_t queque_get = 0;
    sys_main_param_t* sys_task_param = Get_sys_param_pos();
    send_param_t* sys_send_param = Get_send_param_pos();
    memcpy(sys_main_param_old.systemp, sys_task_param->systemp, 3);

    for(;;){
        vTaskDelay(50 / portTICK_PERIOD_MS);
        if(memcmp(sys_task_param, &sys_main_param_old, sizeof(sys_main_param_t)) != 0){
            /* MQTT连接状态 */
            if(sys_task_param->ifappmqttconnect != sys_main_param_old.ifappmqttconnect){
                if(sys_task_param->ifappmqttconnect == MQTT_CONNECT_PRIORITY_H){
                    sys_task_param->ifappmqttconnect = MQTT_APP_CONNECT;
                }
                if(sys_task_param->ifappmqttconnect == MQTT_APP_CONNECT){
                    mqtt_send(MQTT_SEND_D2S_QD, NULL);
                    mqtt_send(MQTT_SEND_D2S_GJ, NULL);
                }
            }
            /* BLUFI BLE 连接状态 */
            if(sys_task_param->ifblufibleconnect != sys_main_param_old.ifblufibleconnect){
                if(sys_task_param->ifblufibleconnect >= BLUFI_BLE_CONNECT){
                    if(sys_task_param->ifblufibleconnect == BLE_CONNECT_PRIORITY_H){
                        ble_registration_json_send();
                        sys_task_param->ifblufibleconnect = BLUFI_BLE_CONNECT;
                    }
                }

            }
            /* 模式 */
            if(sys_task_param->sys_mode != sys_main_param_old.sys_mode){
          
                if(sys_task_param->sys_mode == MODE_XU){
                    mainpwm_xushi_set(XU_MODE);
                }else{
                    mainpwm_xushi_set(SHI_MODE);
                }

            }
            /* 预定时间 */
            if(sys_task_param->sys_runtime != sys_main_param_old.sys_runtime){
                sys_task_param->sys_countdown = sys_task_param->sys_runtime;
            }
            /* 强度 */
            if(sys_task_param->sys_strength != sys_main_param_old.sys_strength){
           
            }
            /* 温度 */
            if(memcmp(sys_task_param->systemp, sys_main_param_old.systemp, 3) != 0){
                save_device_temp_nvs_custom(sys_task_param->systemp);
            }
            /* 剩余时间 */
            if(sys_task_param->sys_countdown != sys_main_param_old.sys_countdown){
                if(sys_task_param->sys_countdown == 0){
                    sys_task_param->sys_status = SYS_STOP;
                    beep_output(BEEP_STOP);
                }
           
            }
            /* 报错 */
            if(sys_task_param->sys_error_type != sys_main_param_old.sys_error_type){
                if(sys_task_param->sys_error_type != ERROR_TYPE_NO_ERROR){
                    sys_task_param->sys_status = SYS_PAUSE;
                    beep_output(BEEP_ERROR);
                }
            }

            /* 状态 */
            if(sys_task_param->sys_status != sys_main_param_old.sys_status){
                switch(sys_task_param->sys_status){

                    case SYS_RUN_NORMAL:
                        mainpwm_output_mode(MAINPWM_NORMAL);
                        sys_task_param->sys_error_type = ERROR_TYPE_NO_ERROR;
                        break;
                    case SYS_RUN_FREQCHANGE:
                        mainpwm_output_mode(MAINPWM_DUTYCHANGE);
               
                        break;
                    case SYS_RUN_HIGHTEMP:
                        mainpwm_output_mode(MAINPWM_STOP);
                  
                        break;
                    case SYS_PAUSE:
                        mainpwm_output_mode(MAINPWM_STOP);
                       
                        break;
                    case SYS_STOP:
                        mainpwm_output_mode(MAINPWM_STOP);
                        sys_task_param->sys_countdown = sys_task_param->sys_runtime;
     
                        break;

                    default:
                        break;
                }

            }
            /* 发送 */
            switch(sys_task_param->sys_status){
                case SYS_RUN_NORMAL:
                case SYS_RUN_FREQCHANGE:
                case SYS_RUN_HIGHTEMP:
                    sys_send_param->send_status = SEND_STATUS_RUN;
                    break;
                case SYS_PAUSE:
                    sys_send_param->send_status = SEND_STATUS_PAUSE;
                    break;
                case SYS_STOP:
                    sys_send_param->send_status = SEND_STATUS_STOP;
                    break;
                default:
                    break;
            }
            sys_send_param->send_countdown = sys_task_param->sys_countdown;
            sys_send_param->send_strength = sys_task_param->sys_strength;
            sys_send_param->send_mode = sys_task_param->sys_mode;
            sys_send_param->send_runtime = sys_task_param->sys_runtime;
            sys_send_param->send_error_type = sys_task_param->sys_error_type;
            sys_send_param->send_temp = sys_task_param->systemp[sys_task_param->sys_strength-1];
            /* led同步 */
            queque_get = tm1650_led_datatrans(sys_task_param);
            xQueueSend(tm1650_led_queue, &queque_get, 0);
            /* 同步数据 */
            memcpy(&sys_main_param_old, sys_task_param, sizeof(sys_main_param_t));
            /* 发送状态 */
            if(sys_task_param->ifappmqttconnect == MQTT_APP_CONNECT){
                mqtt_send(MQTT_SEND_D2U_ZT, sys_send_param);
            }
            if(sys_task_param->ifblufibleconnect >= BLUFI_BLE_CONNECT){
                ble_report_json_send(sys_send_param);
            }

        }

    }
}


IRAM_ATTR bool my_timer_on_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
{

    sys_main_param_t* timer1_sys_param = NULL;
    timer1_sys_param = Get_sys_param_pos();
    /* mqttkeepalive计时 */
    if(timer1_sys_param->ifappmqttconnect == MQTT_APP_CONNECT){
        mqtt_keepalive_count++;
        if(mqtt_keepalive_count >= 2*10){
            timer1_sys_param->ifappmqttconnect = MQTT_APP_DISCONNECT;
            mqtt_keepalive_count = 0;
        }
    }else{
        mqtt_keepalive_count = 0;
    }

    /* 运行计时 */
    if(timer1_sys_param->sys_status >= SYS_RUN_NORMAL){
        if(timer1_sys_param->sys_countdown == 0){
            return 1;
        }
        timer1_sys_param->sys_countdown--;
    }
    
    return 1;
}


extern QueueHandle_t mag_220v_queue;
/* 1/1200s */
IRAM_ATTR bool my_timer2_on_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
{
    static uint32_t flag = 0;
    static uint32_t timer2_mag_220v = 0;
    uint32_t timer2_mag_220v_t = 0;
    if(xQueueReceiveFromISR(mag_220v_queue, &timer2_mag_220v_t, 0)){
        switch(timer2_mag_220v_t){
            case MAG_220V_NORMAL:
            case MAG_220V_DUTYCHANGE:
            case MAG_220V_STOP:
                flag = 0;
                timer2_mag_220v = timer2_mag_220v_t;
                break;
            default:
                break;
        }
    }


    switch(timer2_mag_220v){
        case MAG_220V_NORMAL:
            if(flag == 0){
                gpio_set_level(LEDC_OUTPUT_IO1, 0);
                gpio_set_level(LEDC_OUTPUT_IO2, 0);
                gpio_set_level(GPIO_220V1_PORT, 1);    
                gpio_set_level(GPIO_220V2_PORT, 1);    
            }else if((flag>0) && (flag<100)){
                if((flag%100)<14){
                    gpio_set_level(GPIO_220V1_PORT, 1);    
                    gpio_set_level(GPIO_220V2_PORT, 1);  
                }else{
                    gpio_set_level(GPIO_220V1_PORT, 0);    
                    gpio_set_level(GPIO_220V2_PORT, 0);  
                }
            }else{
                if((flag%100)<14){
                    gpio_set_level(LEDC_OUTPUT_IO1, 1);
                    gpio_set_level(LEDC_OUTPUT_IO2, 1);
                }else{
        
                    gpio_set_level(LEDC_OUTPUT_IO1, 0);
                    gpio_set_level(LEDC_OUTPUT_IO2, 0);
                }
            }
            flag++;
            break;
        case MAG_220V_DUTYCHANGE:
            if(flag == 0){
                gpio_set_level(LEDC_OUTPUT_IO1, 0);
                gpio_set_level(LEDC_OUTPUT_IO2, 0);
                gpio_set_level(GPIO_220V1_PORT, 1);    
                gpio_set_level(GPIO_220V2_PORT, 1);     
            }else if((flag>0) && (flag<100)){
                if((flag%100)<7){
                    gpio_set_level(GPIO_220V1_PORT, 1);    
                    gpio_set_level(GPIO_220V2_PORT, 1);     
                }else{
                    gpio_set_level(GPIO_220V1_PORT, 0);    
                    gpio_set_level(GPIO_220V2_PORT, 0);     
                }
            }else if(flag < (1200*1)){
                if((flag%100)<7){
                    gpio_set_level(LEDC_OUTPUT_IO1, 1);
                    gpio_set_level(LEDC_OUTPUT_IO2, 1);
                }else{
                    gpio_set_level(LEDC_OUTPUT_IO1, 0);
                    gpio_set_level(LEDC_OUTPUT_IO2, 0);
                }
            }else if(flag == (1200*1)){
                gpio_set_level(LEDC_OUTPUT_IO1, 0);
                gpio_set_level(LEDC_OUTPUT_IO2, 0);
                gpio_set_level(GPIO_220V1_PORT, 0);    
                gpio_set_level(GPIO_220V2_PORT, 0);   
            }else if(flag >= (1200*5)){
                flag = 0;
                return 1;
            }
            flag++;
            break;

        case MAG_220V_STOP:
            flag = 0;
            gpio_set_level(LEDC_OUTPUT_IO1, 0);
            gpio_set_level(LEDC_OUTPUT_IO2, 0);
            gpio_set_level(GPIO_220V1_PORT, 0);    
            gpio_set_level(GPIO_220V2_PORT, 0);   
            break;
        default:
            break;

    }


    return 1;
}




